package com.hzit.portal.controller;

import com.aliyun.core.annotation.Path;
import com.hzit.common.utils.R;
import com.hzit.portal.entity.ContentEntity;
import com.hzit.portal.service.ContentService;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Controller
public class PortalController {
	@Autowired
	private ContentService contentService;

	//1. 首页显示
	@GetMapping({"/","/index.html"})
	public String index(Model model){
		List<ContentEntity> list = contentService.findAll2();
		model.addAttribute("contentList",list);
		return "index";
	}

	//2. 显示广告列表
	@GetMapping("/content/list")
	@ResponseBody
	public R list(){
		List<ContentEntity> contentEntities = contentService.findAll3();
		return R.ok().put("data",contentEntities);
	}

	//3. 根据id查询广告
	@GetMapping("/content/{id}")
	@ResponseBody
	public R findById(@PathVariable("id") String id){
		ContentEntity contentEntity = contentService.findById(id);
		return R.ok().put("content",contentEntity);
	}

	//4. 流控模式：关联（对关联的资源/content/message2进行访问，超出流控规则时，保护的是主资源/content/message1）
	//主资源
	@GetMapping("/content/message1")
	@ResponseBody
	public R message1(){
		String msg = contentService.message1();
		return R.ok().put("msg",msg);
	}

	//关联资源
	@GetMapping("/content/message2")
	@ResponseBody
	public R message2(){
		String msg = contentService.message2();
		return R.ok().put("msg",msg);
	}

	// 5. 流控模式：链路模式（如果对底层被关联的资源进行限流时，实际上保护的是入口资源）
	// 如果我们设置的流控规则的入口是/content/message3，则此进保护的是/content/message3
	@GetMapping("/content/message3")
	@ResponseBody
	public R message(){
		String msg = contentService.message();
		return R.ok().put("msg",msg);
	}
	// 如果我们设置的流控规则的入口是/content/message4，则此进保护的是/content/message4
	@GetMapping("/content/message4")
	@ResponseBody
	public R message4(){
		// 这里抛出的异常，由springmvc全局异常处理器接管！
		String msg = contentService.message();
		return R.ok().put("msg",msg);
	}
	@GetMapping(value = "/testD")
	public String testD(int id){
		if(id<0){
			throw new RuntimeException("运行异常");
		}
		return "testD";
	}
}
